//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_drones\fn_createDrone.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_type","_name","_pos","_dir","_start_pos","_height","_can_call","_delay","_drone","_drone_timeout","_max_dist","_dummy_drone","_dist","_drone_id"];
PARAMS_1(_type);

_drone = objNull;
_can_call = true;
if (isnil "PAPABEAR") then { PAPABEAR = [playerSide,"HQ"]; };

if (_can_call && (if (!isnil "mp_rights_fnc_hasRoles") then { !([(getPlayerUID player),['leader','admin','drone']] call mp_rights_fnc_hasRoles)} else { false })) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_NOACCESS", group player];
	_can_call = false;
};
if (_can_call && (count GVAR(PLAYER_DRONE) > 0 || !isnull (getConnectedUav player))) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_ALREADYHAVEDRONE", group player];
	_can_call = false;
};
if (_can_call && GVAR(AVAILABLE_DRONES) == 0) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_ALLDRONESDESTROYED", group player];
	_can_call = false;
};
if (_can_call && GVAR(PLAYER_NEXT_REQUEST) > time) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_TOOEARLY", group player,(round((GVAR(PLAYER_NEXT_REQUEST) - time) / 60)) max 1];
	_can_call = false;
};
if (_can_call && GVAR(DRONES_ACTIVE) >= TYR_Drone_Flying) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_DRONEALREADYACTIVE", group player];
	_can_call = false;
};
if (_can_call && !alive player) then {
	PAPABEAR sideChat format [localize "STR_TYR_DRN_PLAYERDEAD", group player];
	_can_call = false;
};
if (_can_call) then {
	INC(GVAR(DRONES_ACTIVE));
	publicVariable QGVAR(DRONES_ACTIVE);
	
	GVAR(DRONE_DIST_REACHED) = false;
	GVAR(PLAYER_NEXT_REQUEST) = time + TYR_Drone_frequency;
	_name = [_type,0] call mso_fnc_getDisplayName;
	_drone_timeout = (time + TYR_Drone_duration);
	GVAR(PLAYER_DRONE) = [_type,_name,objNull,player,name player,time,_drone_timeout];
	TRACE_2("Player Drone",GVAR(PLAYER_DRONE),count GVAR(DRONES_ACTIVE));
	PAPABEAR sideChat format [localize "STR_TYR_DRN_TRANSMITTED", group player];
	_delay = time + 15;
	waituntil {!alive player || _delay < time};
	PAPABEAR sideChat format [localize "STR_TYR_DRN_ONTHEWAY", group player,_name];
	_delay = time + 15;
	waituntil {!alive player || _delay < time};
	if (!alive player) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_PLAYERDEAD", group player]; };
	PAPABEAR sideChat format [localize "STR_TYR_DRN_INCOMING", group player,_name];
	
	_pos = getPos Player;
	_start_pos = [_pos, 2500] call mso_core_fnc_getRanPointCircleOuterAir;
	_dir = [_start_pos, _pos] call mso_core_fnc_dirTo;
	_height = 200;
	_start_pos set [2, _height];
	
	_drone = createVehicle [_type, _start_pos, [], 20, "FLY"];
	_drone setDir _dir;
	_drone engineon true;
	_drone setVelocity [(sin _dir) * 100,(cos _dir) * 100, 1];
	createVehicleCrew _drone;
	GVAR(PLAYER_DRONE) set [2,_drone];
	_drone flyinheight _height;
	group (driver _drone) setCombatMode "BLUE";
	(driver _drone) doFollow player;
	(driver _drone) doMove _pos;
	{ _x setCaptive true;
	} foreach crew _drone;
	
	TRACE_1("Drone",ARR_3(_drone, typeof _drone, crew _drone));
	
	_delay = time + 15;
	waituntil {!alive player || _delay < time || ([_drone,_pos] call mso_core_fnc_groundDist) < 800};
	if (!alive player) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_PLAYERDEAD", group player]; };
	if (!alive _drone) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_DRONEDESTROYED", group player]; GVAR(PLAYER_NEXT_REQUEST) = time + 120;};
	{ _x setCaptive false;
	} foreach crew _drone;
	group (driver _drone) setCombatMode "GREEN";
	if (!(player connectTerminalToUav _drone)) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_FAILEDCONNECTION", group player]; };
	PAPABEAR sideChat format [localize "STR_TYR_DRN_READY", group player,(round((_drone_timeout - time) / 60)) max 1];
	PAPABEAR sideChat format [localize "STR_TYR_DRN_MAXDIST", group player,TYR_Drone_Max_Dist];
	_max_dist = TYR_Drone_Max_Dist * 1000;
	waituntil {sleep 0.778; !alive player || _drone_timeout < time || !alive _drone || ([_drone,_max_dist] call FUNC(droneTestDistance)) || !isUavConnected _drone};
	if (!alive player) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_PLAYERDEAD", group player]; };
	if (!alive _drone) exitWith {
		DEC(GVAR(AVAILABLE_DRONES));
		publicVariable QGVAR(AVAILABLE_DRONES);
		PAPABEAR sideChat format [localize "STR_TYR_DRN_DRONEDESTROYED", group player];
	};
	if (([_drone,player] call mso_core_fnc_groundDist) > _max_dist) exitWith { PAPABEAR sideChat format [localize "STR_TYR_DRN_LOSTCONNECTION", group player]; };
	if (isUavConnected _drone) then {
		scriptDone ([player,_drone] spawn FUNC(disconnectDrone));
		sleep 5;
	};
	PAPABEAR sideChat format [localize "STR_TYR_DRN_TIMEISUP", group player];
};
if (_can_call) then {
	DEC(GVAR(DRONES_ACTIVE));
	publicVariable QGVAR(DRONES_ACTIVE);
};
GVAR(PLAYER_DRONE) = [];
if (!isnil "_drone" && {!isNull _drone}) then {
	if (alive _drone && {canMove _drone}) then {
		_pos = [getPos _drone, 1000] call mso_core_fnc_getRanPointCircleOuterAir;
		if (isUavConnected _drone) then {
			scriptDone ([player,_drone] spawn FUNC(disconnectDrone));
			sleep 5;
		};
		(driver _drone) doMove _pos;
		sleep 5;
		(driver _drone) doMove _pos;
		sleep 5;
		_delay = time + 15;
		_crew = crew _drone;
		waituntil {!alive _drone || !canMove _drone || (_drone distance _pos) < 100 || _delay < time};
		if (alive _drone) then {
			_crew = crew _drone;
			{ if (!isnull _x) then { _x setDamage 1.1 };
			} foreach _crew;
			sleep 1;
			{ if (!isnull _x) then { deleteVehicle _x };
			} foreach _crew;
			_drone setPos [0,0,1000];
			_drone setDamage 1.1;
			deleteVehicle _drone;
		};
	};
};
TRACE("End");
TMFPROFILERSTOP;
